package com.uh.calculation.bean;


import com.uh.calculation.annotation.Conf;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Bean;
import java.io.Serializable;
import java.lang.reflect.Field;

@Data
/**
 * 符号解析器
 */
public class ContentBean implements Serializable {
    private static final long serialVersionUID = 1L;
    @Conf(value = "\\]")
    private String  dayu;
    @Conf(value = "\\[")
    private String  xiaoyu;
    @Conf(value = "=")
    private String  shuzhi;

    private String  num;

    public  ContentBean(String content){
        Field[] fields = this.getClass().getDeclaredFields();
        String fieldVal;
        for (Field field : fields) {
            field.setAccessible(true);
            String s[];
            try {
                if(!field.getName().equals("serialVersionUID")) {
                    String  groupName = "";
                    if(field.isAnnotationPresent(Conf.class)){
                        Conf sa = (Conf)field.getAnnotation(Conf.class);//得到注解
                        //得到注解的值
                        groupName=sa.value();
                        s=content.split(groupName);
                        if(s.length==1){
                            s=content.split(groupName);
                        }
                        if(s.length>1) {
                            fieldVal=getVal(s[1],field.getName());
                            field.setAccessible(true);
                            field.set(this, fieldVal);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            field.setAccessible(false);
        }
    }
    private  String  getVal(String val,String fieldName) {
        String str[] = {"\\[","\\]","="};
        String v[];
        for(String st : str) {
            v=val.split(st);
            if(v.length==1){
                v=val.split(st);
            }
            if(v.length>1){
                val=v[0];
            }
        }
        return  val;
    }


    public  String  getNum(){
        if(!StringUtils.isEmpty(dayu)){
            return  this.dayu;
        }else {
            return this.xiaoyu;
        }
    }





}
